from werkzeug import ETagRequestMixin, Request, Response
from werkzeug.wsgi import wrap_file
from flatout import flatout
from flatout import ohill
from pickupliners import arb

csstempbufs,jstempbufs,htmltempbufs = ['css/bf_static.css'],['js/2ZeroClipboard_nb_user_template.js'],['2zcb_nb_template.html']

# This be the one that goes into the func to produce arb output
ifile = ['liners.txt.sh']
# Static Htmls
stathtmls = ['2zcb_init_user_template.html','2zcb_close_user_template.html','css/bf_static.css','js/ZeroClipboard.js','js/2ZeroClipboard_init_user_template.js']


alsofiles = csstempbufs + jstempbufs + htmltempbufs + ifile + stathtmls

thing = arb()
elem_bufs = thing.parse(ifile)
elem_bufct = len(elem_bufs) - 1

def testout():
    thing = ohill()
    cssthing = thing.csstems(csstempbufs,1)
    jsthing = thing.jstems(jstempbufs,elem_bufct)
    bodything = thing.bodytems(htmltempbufs,elem_bufct,elem_bufs)
    totalout = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'
    totalout += cssthing
    totalout += jsthing
    totalout += bodything  
    return totalout

out = testout()
##
# Now to create the edit-save out of the lbuffermod and associated bdpcon forms
##
# Also - clean up your included wsgi-handlers and consider js/templating sanity

class briefly(object):
    def __init__(self,out):     
        self.arb = Response(out, mimetype='text/html')
    def qwhat(self,request):
        return self.arb
    def wsgi_app(self, environ, start_response):
        request = Request(environ)
        if request.url == 'http://127.0.0.1:5000/ZeroClipboard.swf':
            ofile = open('ZeroClipboard.swf','r')
            response = Response(wrap_file(environ, ofile, buffer_size=8192), mimetype='application/xhtml+xml', direct_passthrough=True)
            response.cache_control.public=True
            response.cache_control.max_age=360000
#            print request.cache_control.provided
            if (request.cache_control.provided):
                print "TRUE"
            #print "TRUE::" + dir(request.cache_control.provided) 
            #print response.cache_control.public
            #print response.cache_control.max_age
            #print response.cache_control.to_header
            #print dir(response.cache_control)     
            return response(environ, start_response)
        if request.url == 'http://127.0.0.1:5000/css/spritesheet.png?2012-11-29T10:48':
            ofile = open('css/images/spritesheet.png','r')
            response = Response(wrap_file(environ, ofile, buffer_size=8192), mimetype='image/png', direct_passthrough=True)
            response.cache_control.public=True
            response.cache_control.max_age=360000
            return response(environ, start_response)
        if request.url == 'http://127.0.0.1:5000/css/images/thatkitty.png':
            ofile = open('css/images/thatkitty.png','r')
            response = Response(wrap_file(environ, ofile, buffer_size=8192), mimetype='image/png', direct_passthrough=True)
            response.cache_control.public=True
            response.cache_control.max_age=360000
            return response(environ, start_response)
        if request.url == 'http://127.0.0.1:5000/ajax.ajax':
            xplore = ''
            #for x in dir(request.cookies):
            #    xplore += x + '\n'
            #xplore += str(request.cookies.items())
            #xplore += str(request.cookies.viewkeys()) + '\n'   
            #xplore += str(request.cookies.viewvalues())
## something like handle_form([NAMES],[PARAMS]) where NAMES is a sanity-checked set of classes that accept PARAMS ##
            xplore += str((request.form.viewvalues()))
            aresponse = Response(xplore,mimetype='text/html')
            return aresponse(environ, start_response)
        response = self.arb
        #response.set_cookie('testcookie',value='uno',max_age=None,expires=None,path='/',domain=None,secure=None,httponly=False)
        return response(environ, start_response)

    def __call__(self, environ, start_response):
        return self.wsgi_app(environ, start_response)

def create_lapp():
#    lapp = testout()
    lapp = briefly(out)
    return lapp

if __name__ == '__main__':
    from werkzeug.serving import run_simple
    lapp = create_lapp()
    run_simple('127.0.0.1', 5000, lapp, use_debugger=True,use_reloader=True,extra_files=alsofiles)

